using System;
using System.Collections.Generic;
using System.IO;
using AbpZero.EntityFrameworkCore;
using AbpZero.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;

namespace AbpZero
{
    [DependsOn(
        typeof(AbpZeroApplicationModule),
        typeof(AbpZeroEntityFrameworkCoreModule),
        typeof(AbpZeroHttpApiModule),
        typeof(AbpAutofacModule),
        typeof(AbpAspNetCoreSerilogModule)
    )]
    public class AbpZeroHttpApiHostModule : AbpModule
    {
        private const string DefaultCorsPolicyName = "Default";

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var hostingEnvironment = context.Services.GetHostingEnvironment();
            var configuration = context.Services.GetConfiguration();

            context.Services.AddSwaggerGen(
                options =>
                {
                    options.SwaggerDoc("v1", new OpenApiInfo {Title = "AbpZero API", Version = "v1"});
                    options.DocInclusionPredicate((docName, description) => true);
                    options.CustomSchemaIds(type => type.FullName);
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, "AbpZero.Application.xml");
                    options.IncludeXmlComments(xmlPath, true);
                });

            context.Services.AddCors(options =>
            {
                options.AddPolicy(DefaultCorsPolicyName, builder =>
                {
                    builder
                        .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                });
            });

            context.Services.Configure<AbpAspNetCoreMvcOptions>(options =>
            {
                options.ConventionalControllers.Create(typeof(AbpZeroApplicationModule).Assembly);
            });

            context.Services.AddMvc(options =>
            {
                options.Filters.ReplaceOne(x => (x as ServiceFilterAttribute)?.ServiceType?.Name == nameof
                    (AbpExceptionFilter), new ServiceFilterAttribute(typeof(ExceptionFilter)));
            });

            context.Services.AddSpaStaticFiles(options => options.RootPath = "wwwroot");
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();

            if (env.IsDevelopment())
                app.UseDeveloperExceptionPage();
            else
                app.UseHsts();

            app.UseCorrelationId();
            app.UseVirtualFiles();
            app.UseRouting();
            app.UseCors(DefaultCorsPolicyName);

            app.UseSwagger();
            app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support APP API"); });
            app.UseAbpSerilogEnrichers();
            app.UseConfiguredEndpoints();

            app.UseSpa(options => { options.Options.SourcePath = "ClientApp/"; });
        }
    }
}